﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using System.Data.Entity;
using HHG.Common; 
using OnLineExam.Web.Models.ExcelViewModels;
 

namespace OnLineExam.Web.mag.old
{
    public partial class ProblemMag : BasePage
    {
        private OnLineExam.Data.OnlineExamEntities _dbContext;
        private OnLineExam.Data.EfRepository<OnLineExam.Data.Paper> _paperRepository;
        private OnLineExam.Data.EfRepository<OnLineExam.Data.SingleProblem> _sRepository;

        private OnLineExam.Data.EfRepository<OnLineExam.Data.MultiProblem> _mRepository;
        private OnLineExam.Data.EfRepository<OnLineExam.Data.JudgeProblem> _jRepository;
        private OnLineExam.Data.EfRepository<OnLineExam.Data.FillBlankProblem> _fRepository;
        private OnLineExam.Data.EfRepository<OnLineExam.Data.QuestionProblem> _qRepository;


        public ProblemMag()
        {
            _dbContext = new OnLineExam.Data.OnlineExamEntities();
            _paperRepository = new OnLineExam.Data.EfRepository<OnLineExam.Data.Paper>(_dbContext);
            _sRepository = new OnLineExam.Data.EfRepository<OnLineExam.Data.SingleProblem>(_dbContext);
            _mRepository = new OnLineExam.Data.EfRepository<OnLineExam.Data.MultiProblem>(_dbContext);
            _jRepository = new OnLineExam.Data.EfRepository<OnLineExam.Data.JudgeProblem>(_dbContext);
            _fRepository = new OnLineExam.Data.EfRepository<OnLineExam.Data.FillBlankProblem>(_dbContext);
            _qRepository = new OnLineExam.Data.EfRepository<OnLineExam.Data.QuestionProblem>(_dbContext);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindPaper();

            }
        }
        void BindPaper()
        {
            using (var ctx = new OnLineExam.Data.OnlineExamEntities())
            {
                var data = ctx.Paper.AsNoTracking().AsQueryable();
                if (base.GetCurrentUser().RoleName != "管理员")
                    data = data.Where(s => s.DepID == GetCurrentUser().DepID);

                this.ddlPaper.DataTextField = "PaperName";
                this.ddlPaper.DataValueField = "PaperID";
                this.ddlPaper.DataSource = data.ToList();
                this.ddlPaper.DataBind();
                this.ddlPaper.Items.Insert(0, new ListItem("请选择", ""));

            }



        }
        void ImportData(string file, OnLineExam.Data.Paper paper)
        {

            Npoi.Mapper.Mapper mapper = new Npoi.Mapper.Mapper(file);

            if (1 == 1)
            {
                
                var objectInfos = mapper.Take<Models.ExcelViewModels.SingleProblemModel>("单选题");


                foreach (var item in objectInfos)
                {
                    var temp = item.Value.GetSingleProblem(paper.PaperID);
                    if (temp.Title.HasValue())
                        _dbContext.SingleProblem.Add(temp);
                }
            }
             

            if (1 == 1)
            {
                var objectInfos = mapper.Take<Models.ExcelViewModels.MultiProblemModel>("多选题");

                 
                foreach (var item in objectInfos)
                {
                    var temp = item.Value.GetMultiProblem(paper.PaperID);
                    if(temp.Title.HasValue())
                    _dbContext.MultiProblem.Add(temp);
                }
            }
            
            if (1 == 1)
            {
                var objectInfos = mapper.Take<Models.ExcelViewModels.JudgeProblemModel>("判断题");


                foreach (var item in objectInfos)
                {
                    var temp = item.Value.GetJudgeProblem(paper.PaperID);
                    if (temp.Title.HasValue())
                        _dbContext.JudgeProblem.Add(temp);
                }
            }
          
            if (1 == 1)
            {
                var objectInfos = mapper.Take<Models.ExcelViewModels.FillBlankProblemModel>("填空题");


                foreach (var item in objectInfos)
                {
                    var temp = item.Value.GetFillBlankProblem(paper.PaperID);
                    if (temp.Title.HasValue())
                        _dbContext.FillBlankProblem.Add(temp);
                }
            }
             
            if (1 == 1)
            {
                var objectInfos = mapper.Take<Models.ExcelViewModels.QuestionProblemModel>("问答题");


                foreach (var item in objectInfos)
                {
                    var temp = item.Value.GetQuestionProblem(paper.PaperID);
                    if (temp.Title.HasValue())
                        _dbContext.QuestionProblem.Add(temp);
                }
            }
			_dbContext.Configuration.ValidateOnSaveEnabled = false;
			int n=  _dbContext.SaveChanges();

            HHG.Common.MessageBox.Show(this, "导入成功" + n);


        }

        protected void Button1_Click(object sender, EventArgs e)
        {



            if (string.IsNullOrWhiteSpace(ddlPaper.SelectedValue))
                return;

            var paper = _paperRepository.GetById(ddlPaper.SelectedValue.GetInt());

             

            string depid = paper.DepID + "";
            string courseid = paper.CourseID + "";


            var dic = Server.MapPath("~/Upload/problem/");
            try
            {
                System.IO.Directory.CreateDirectory(dic);
            }
            catch (Exception)
            {


            }



            string filename = HHG.Common.UploadFile.sendFileUpload(".xls", 0, dic, this.FileUpload1);

            ImportData(dic + filename, paper);
            return;

        }
        protected void ddlCourse_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}